package cn.bocweb.spinning.view.fragment;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CollectAnswerBean;
import cn.bocweb.spinning.model.Bean.CollectKeyBean;
import cn.bocweb.spinning.model.Bean.CollectListBean;
import cn.bocweb.spinning.model.Bean.MyTestCollectBean;
import cn.bocweb.spinning.model.Bean.SequenceAnswerBean;
import cn.bocweb.spinning.model.Bean.SequenceKeysBean;
import cn.bocweb.spinning.model.Bean.SequenceSyncForLoginBean;
import cn.bocweb.spinning.model.Bean.SequenceTestBean;
import cn.bocweb.spinning.model.Bean.TrainRandomBean;
import cn.bocweb.spinning.model.Bean.TrainRandomKeysBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.ExerciseActivity;
import cn.bocweb.spinning.view.activity.ExerciseActivity;

import cn.bocweb.spinning.view.activity.SequenceTestActivity;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 15/12/26.
 */
public class ExerciseFragment extends Fragment implements View.OnClickListener {

    SequenceSyncForLoginBean sequenceSyncForLoginBean;
    String ttype;
    String title;
    List<String> right;
    List<String> choose = new ArrayList<>();
    List<String> option;
    String explain;
    String personRate;
    String allRate;
    int quizNum = 0;

    CollectAnswerBean collectAnswerBean;
    SequenceAnswerBean sequenceAnswerBean;
    StringBuffer results = new StringBuffer();
    UserPresenter userPresenter;
    CollectKeyBean.DataEntity testKeys;
    SequenceTestBean.DataEntity dataEntity;
    MyTestCollectBean.DataEntity collectDataEntity;
    ExerciseActivity exerciseActivity;
    Timer timer;
    String tid;
    int position, type, total, isRightInt = 2, collectPosition = 0;
    String userid, libtype, id, myWrong, myRight, result;
    boolean isCheckExplain, isVisible = false, isRight = false, isSelected = false, ischecked = false, notLoginGetAnswer = false;
    boolean isSelectAnswer = false, isCollect = false;
    @Bind(R.id.train_type)
    TextView trainType;
    @Bind(R.id.train_title)
    TextView trainTitle;
    @Bind(R.id.train_select_icon_a)
    ImageView trainSelectIconA;
    @Bind(R.id.train_select_answer_a)
    TextView trainSelectAnswerA;
    @Bind(R.id.train_select_a)
    LinearLayout trainSelectA;
    @Bind(R.id.train_select_icon_b)
    ImageView trainSelectIconB;
    @Bind(R.id.train_select_answer_b)
    TextView trainSelectAnswerB;
    @Bind(R.id.train_select_b)
    LinearLayout trainSelectB;
    @Bind(R.id.train_select_icon_c)
    ImageView trainSelectIconC;
    @Bind(R.id.train_select_answer_c)
    TextView trainSelectAnswerC;
    @Bind(R.id.train_select_c)
    LinearLayout trainSelectC;
    @Bind(R.id.train_select_icon_d)
    ImageView trainSelectIconD;
    @Bind(R.id.train_select_answer_d)
    TextView trainSelectAnswerD;
    @Bind(R.id.train_select_d)
    LinearLayout trainSelectD;
    @Bind(R.id.train_select_icon_e)
    ImageView trainSelectIconE;
    @Bind(R.id.train_select_answer_e)
    TextView trainSelectAnswerE;
    @Bind(R.id.train_select_e)
    LinearLayout trainSelectE;
    @Bind(R.id.train_select_icon_f)
    ImageView trainSelectIconF;
    @Bind(R.id.train_select_answer_f)
    TextView trainSelectAnswerF;
    @Bind(R.id.train_select_f)
    LinearLayout trainSelectF;
    @Bind(R.id.train_judge_icon_a)
    ImageView trainJudgeIconA;
    @Bind(R.id.train_judge_answer_a)
    TextView trainJudgeAnswerA;
    @Bind(R.id.train_judge_a)
    LinearLayout trainJudgeA;
    @Bind(R.id.train_judge_icon_b)
    ImageView trainJudgeIconB;
    @Bind(R.id.train_judge_answer_b)
    TextView trainJudgeAnswerB;
    @Bind(R.id.train_judge_b)
    LinearLayout trainJudgeB;
    @Bind(R.id.train_judge)
    LinearLayout trainJudge;
    @Bind(R.id.train_allrate)
    TextView trainAllrate;
    @Bind(R.id.sequence_wrong_people)
    TextView sequenceWrongPeople;
    @Bind(R.id.train_personrate)
    TextView trainPersonrate;
    @Bind(R.id.sequence_wrong_my_wrong)
    TextView sequenceWrongMyWrong;
    @Bind(R.id.sequence_wrong_my_right)
    TextView sequenceWrongMyRight;
    @Bind(R.id.train_explain_rate)
    LinearLayout trainExplainRate;
    @Bind(R.id.train_explain)
    TextView trainExplain;
    @Bind(R.id.train_explain_detail)
    LinearLayout trainExplainDetail;
    @Bind(R.id.sequence_wrong_layout)
    LinearLayout sequenceWrongLayout;
    @Bind(R.id.sequence_explain_explain)
    TextView sequenceExplainExplain;
    @Bind(R.id.sequence_explain_detail)
    LinearLayout sequenceExplainDetail;
    @Bind(R.id.sequence_explain_allrate)
    TextView sequenceExplainAllrate;
    @Bind(R.id.sequence_explain_people_wrong)
    TextView sequenceExplainPeopleWrong;
    @Bind(R.id.sequence_explain_personrate)
    TextView sequenceExplainPersonrate;
    @Bind(R.id.sequence_explain_my_wrong)
    TextView sequenceExplainMyWrong;
    @Bind(R.id.sequence_explain_my_right)
    TextView sequenceExplainMyRight;
    @Bind(R.id.sequence_explain_rate)
    LinearLayout sequenceExplainRate;
    @Bind(R.id.sequence_explain_layout)
    LinearLayout sequenceExplainLayout;
    @Bind(R.id.train_title_layout)
    FrameLayout trainTitleLayout;
    @Bind(R.id.train_select)
    LinearLayout trainSelect;

    Timer timer1;


    @SuppressLint("ValidFragment")
    public ExerciseFragment(int position, int type, String userid, String libtype, int total) {

        this.position = position;
        this.type = type;
        this.userid = userid;
        this.libtype = libtype;
        this.total = total;
    }

    public SequenceSyncForLoginBean getSequenceSyncForLoginBean() {
        return sequenceSyncForLoginBean;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_train_select, container, false);
        ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        userPresenter = new UserPresenterImp();
        timer = new Timer();
        exerciseActivity = (ExerciseActivity) getActivity();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        switch (type) {
            case Constant.SEQUENCE: {
                if (isVisible) {
                    if (!EventBus.getDefault().isRegistered(this)) {
                        EventBus.getDefault().register(this);
                    }
                    timer = new Timer();
                    userPresenter = new UserPresenterImp();
                    if (!notLoginGetAnswer) {
                        TimerTask timerTask = new TimerTask() {
                            @Override
                            public void run() {
                                userPresenter.sequenceTest(position, libtype, userid);
                            }
                        };
                        if (timer == null) {
                            timer = new Timer();
                            timer.schedule(timerTask, 500);
                        } else {
                            timer = new Timer();
                            timer.schedule(timerTask, 500);
                        }
                    } else {
                        SequenceAnswerBean bean1 = new SequenceAnswerBean();
                        bean1.setChoose(choose);
                        bean1.setIsRight(String.valueOf(isRightInt));
                        initType();
                        initTitle();
                        initOptions();
                        initQuizNum();
                        initInfo(bean1);
                        if (!isCheckExplain) {
                            trainExplain.setText(explain);
                            trainAllrate.setText(allRate + "%");
                            sequenceWrongPeople.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceWrongMyWrong.setText("0");
                                sequenceWrongMyRight.setText("0");
                                trainPersonrate.setText("0%");
                            } else {
                                trainPersonrate.setText(personRate + "%");
                                sequenceWrongMyWrong.setText(myWrong);
                                sequenceWrongMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.GONE);
                            sequenceWrongLayout.setVisibility(View.VISIBLE);
                        } else {
                            sequenceExplainExplain.setText(explain);
                            sequenceExplainAllrate.setText(allRate + "%");
                            sequenceExplainPeopleWrong.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceExplainMyRight.setText("0");
                                sequenceExplainMyWrong.setText("0");
                                sequenceExplainPersonrate.setText("0%");
                            } else {
                                sequenceExplainPersonrate.setText(personRate + "%");
                                sequenceExplainMyWrong.setText(myWrong);
                                sequenceExplainMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.VISIBLE);
                            sequenceWrongLayout.setVisibility(View.GONE);
                        }
                    }
                    if (sequenceAnswerBean != null) {
                        initInfo(sequenceAnswerBean);
                    }
                } else {
                    if (EventBus.getDefault().isRegistered(this)) {
                        EventBus.getDefault().unregister(this);
                    }
                    if (timer != null) {
                        timer.cancel();
                        timer.purge();
                    }
                    onPause();
                }
                break;
            }
            case Constant.TRAIN_ERROR: {
                if (isVisible) {
                    if (!EventBus.getDefault().isRegistered(this)) {
                        EventBus.getDefault().register(this);
                    }
                    userPresenter = new UserPresenterImp();
                    TimerTask timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            userPresenter.errorTrain(String.valueOf(position + 1), libtype, userid);
                        }
                    };
                    if (timer == null) {
                        timer = new Timer();
                    }
                    timer.schedule(timerTask, 500);
                }
                break;
            }

            case Constant.COLLECT: {
                if (isVisible) {
                    if (!EventBus.getDefault().isRegistered(this)) {
                        EventBus.getDefault().register(this);
                    }
                    userPresenter = new UserPresenterImp();
                    TimerTask timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            userPresenter.collectQuestion(position, userid, libtype);
                        }
                    };
                    if (collectAnswerBean == null) {
                        collectAnswerBean = new CollectAnswerBean();
                        if (timer == null) {
                            timer = new Timer();
                        }
                        timer.schedule(timerTask, 500);
                    } else {
                        initInfo(collectAnswerBean);
                    }

                }
                break;
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
        if (timer != null) {
            timer.cancel();
            timer.purge();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer.purge();
        }
    }

    public String getTId() {
        return id;
    }
//    public int getQuizNum() {
//        return quizNum;
//    }
//    public void setQuizNum(int quizNum) {
//        this.quizNum = quizNum;
//    }

    public String getResult() {
        return result;
    }

    public int getType() {
        return type;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getLibtype() {
        return libtype;
    }

    public boolean isRight() {
        return isRight;
    }

    public void onEventMainThread(TrainRandomBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                if (exerciseActivity == null) {
                    exerciseActivity = (ExerciseActivity) getActivity();
                }
                exerciseActivity.isCollect(bean.getRecord().getIsCollect().equals("1"));
                tid = bean.getData().getId();
                ttype = bean.getData().getTtype();
                option = bean.getData().getOption();
                right = bean.getData().getRight();
                title = bean.getData().getTitle();
                id = bean.getData().getId();

                allRate = bean.getRecord().getAllrate();

                if (bean.getRecord().getPersonrate() != null) {
                    personRate = bean.getRecord().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getRecord().getExplain();
                if (bean.getRecord().getWrong_times() != null) {
                    myWrong = bean.getRecord().getWrong_times();
                } else {
                    myWrong = "0";
                }
                if (bean.getRecord().getOk_times() != null) {
                    myRight = bean.getRecord().getOk_times();
                } else {
                    myRight = "0";
                }

                initType();
                initTitle();
                initOptions();
                initQuizNum();

                if (isSelected) {
                    switch (bean.getData().getTtype()) {
                        case "1": {
                            initSelectResult(Integer.parseInt(result));
                            break;
                        }
                        case "2": {
                            initJudgeResult(Integer.parseInt(result));
                            break;
                        }
                        case "3": {
                            initSelectsResult(result);
                            break;
                        }
                    }
                    checkExplain();

                } else {
                    initClickBtn();
                }
            } else if (bean.getReturn_code() == -206) {
                DeviceUtil.Log(bean.getMsg());
            }
        }
    }

    private void checkExplain() {
        if (!isCheckExplain) {
            trainExplain.setText(explain);
            trainAllrate.setText(allRate + "%");
            trainPersonrate.setText(personRate + "%");
            sequenceWrongMyRight.setText(myRight);
            sequenceWrongMyWrong.setText(myWrong);
            sequenceWrongPeople.setText(allRate + "%");
            sequenceWrongLayout.setVisibility(View.VISIBLE);
            sequenceExplainLayout.setVisibility(View.GONE);
        } else {
            sequenceExplainPersonrate.setText(personRate + "%");
            sequenceExplainAllrate.setText(allRate + "%");
            sequenceExplainExplain.setText(explain);
            sequenceExplainPeopleWrong.setText(allRate + "%");
            sequenceExplainMyRight.setText(myRight);
            sequenceExplainMyWrong.setText(myWrong);
            sequenceWrongLayout.setVisibility(View.GONE);
            sequenceExplainLayout.setVisibility(View.VISIBLE);
        }

    }

    private void initSelectsResult(String result) {
        isSelectAnswer = true;
        StringBuffer selectRight = new StringBuffer();

        choose.add(result);
        this.result = String.valueOf(result);
        for (int i = 0; i < right.size(); i++) {
            selectRight.append(right.get(i));
        }
        String selectAnswer = selectRight.toString();

        if (result.equals(selectAnswer)) {

            if (!isSelected) {
                switch (type) {
                    case 1: {
                        userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "1", String.valueOf(result), userid);
//                        userPresenter.trainRandomKeys(String.valueOf(position + 1), id, ttype, libtype, "1", String.valueOf(result), userid);
                        break;
                    }
                    case 2: {
                        userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "1", String.valueOf(result), userid);
                        if (!exerciseActivity.isAllAnswer()) {
                            if (position < total - 1) {
                                exerciseActivity.addQuizCorrect(position, true);
                                TimerTask timerTask = new TimerTask() {
                                    @Override
                                    public void run() {
                                        exerciseActivity.selectRightAnswer(position + 1);
                                    }
                                };
                                timer.schedule(timerTask, 1000);
                                DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
                            }
                        }
                        break;
                    }
                    case 3: {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(result), "1");
                        break;
                    }
                }

            }

            for (int i = 0; i < result.length(); i++) {
                switch (result.charAt(i)) {
                    case 1: {
                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                    case 2: {
                        trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                    case 3: {
                        trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                    case 4: {
                        trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                    case 5: {
                        trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                    case 6: {
                        trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                        break;
                    }
                }

            }
        } else {
            if (!isSelected) {
                switch (type) {
                    case 1: {
//                        userPresenter.trainRandomKeys(String.valueOf(position + 1), id, ttype, libtype, "0", String.valueOf(result), userid);
                        userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "0", String.valueOf(result), userid);
                        break;
                    }
                    case 2: {
                        userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "0", String.valueOf(result), userid);
                        break;
                    }
                    case 3: {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, result, "0");
//                        userPresenter.hardKeys(String.valueOf(position + 1), id, ttype, libtype, "0", userid);
                        break;
                    }
                }
            }
            exerciseActivity.addQuizCorrect(position, false);
            trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
            trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
            for (int i = 0; i < selectAnswer.length(); i++) {
                switch (selectAnswer.charAt(i)) {
                    case 1: {
                        drawSelectsResult(trainSelectIconA);
                        break;
                    }
                    case 2: {
                        drawSelectsResult(trainSelectIconB);
                        break;
                    }
                    case 3: {
                        drawSelectsResult(trainSelectIconC);
                        break;
                    }
                    case 4: {
                        drawSelectsResult(trainSelectIconD);
                        break;
                    }
                    case 5: {
                        drawSelectsResult(trainSelectIconE);
                        break;
                    }
                    case 6: {
                        drawSelectsResult(trainSelectIconF);
                        break;
                    }
                }
            }
        }
        isSelected = true;
    }

    public void onEventMainThread(TrainRandomKeysBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {

                allRate = bean.getData().getAllrate();
                if (bean.getData().getPersonrate() != null) {
                    personRate = bean.getData().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getData().getExplain();
                if (bean.getData().getWrong_times() != null) {
                    myWrong = bean.getData().getWrong_times();
                } else {
                    myWrong = "0";
                }
                if (bean.getData().getOk_times() != null) {
                    myRight = bean.getData().getOk_times();
                } else {
                    myRight = "0";
                }

                trainExplain.setText(explain);
                trainAllrate.setText(allRate + "%");
                trainPersonrate.setText(personRate + "%");
                sequenceWrongMyRight.setText(myRight);
                sequenceWrongMyWrong.setText(myWrong);
                sequenceWrongPeople.setText(allRate + "%");

                sequenceExplainLayout.setVisibility(View.GONE);
                sequenceWrongLayout.setVisibility(View.VISIBLE);
            }
        }
    }

    private void initType() {
        trainTitleLayout.setVisibility(View.VISIBLE);
        if (!(ttype == null || ttype.isEmpty())) {
            switch (Integer.parseInt(ttype)) {
                case 1: {
                    trainType.setText("【单选】");
                    trainSelect.setVisibility(View.VISIBLE);
                    trainJudge.setVisibility(View.GONE);
                    break;
                }
                case 2: {
                    trainType.setText("【判断】");
                    trainSelect.setVisibility(View.GONE);
                    trainJudge.setVisibility(View.VISIBLE);
                    break;
                }
                case 3: {
                    trainType.setText("【不定项】");
                    trainSelect.setVisibility(View.GONE);
                    trainJudge.setVisibility(View.VISIBLE);
                    break;
                }
                default: {
                    // TODO: 16/1/25 这里要做成下拉刷新
                }
            }
        }
    }

    private void initOptions() {

        quizNum = 0;
        if (Integer.parseInt(ttype) != 2) {
            for (int i1 = 0; i1 < option.size(); i1++) {
                if (option != null && !option.get(i1).equals("")) {
                    quizNum++;
                }
            }
        }
        if (!(option == null || option.isEmpty())) {
            switch (quizNum) {
                case 1:
                    trainSelectAnswerA.setText(option.get(0));
                    break;
                case 2:
                    trainSelectAnswerA.setText(option.get(0));
                    trainSelectAnswerB.setText(option.get(1));
                    break;
                case 3:
                    trainSelectAnswerA.setText(option.get(0));
                    trainSelectAnswerB.setText(option.get(1));
                    trainSelectAnswerC.setText(option.get(2));
                    break;
                case 4:
                    trainSelectAnswerA.setText(option.get(0));
                    trainSelectAnswerB.setText(option.get(1));
                    trainSelectAnswerC.setText(option.get(2));
                    trainSelectAnswerD.setText(option.get(3));
                    break;
                case 5:
                    trainSelectAnswerA.setText(option.get(0));
                    trainSelectAnswerB.setText(option.get(1));
                    trainSelectAnswerC.setText(option.get(2));
                    trainSelectAnswerD.setText(option.get(3));
                    trainSelectAnswerE.setText(option.get(4));
                    break;
                case 6:
                    trainSelectAnswerA.setText(option.get(0));
                    trainSelectAnswerB.setText(option.get(1));
                    trainSelectAnswerC.setText(option.get(2));
                    trainSelectAnswerD.setText(option.get(3));
                    trainSelectAnswerE.setText(option.get(4));
                    trainSelectAnswerF.setText(option.get(5));
                    break;
                default:
                    break;
            }

        }

    }

    private void initTitle() {
        trainTitle.setText("\t\t\t\t\t         " + title);
    }

    public void onEventMainThread(CollectKeyBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                allRate = bean.getData().getAllrate();
                personRate = bean.getData().getPersonrate();
                myRight = bean.getData().getOk_times();
                myWrong = bean.getData().getWrong_times();
                explain = bean.getData().getExplain();

                trainExplainRate.setVisibility(View.VISIBLE);
                trainExplainDetail.setVisibility(View.VISIBLE);

                trainAllrate.setText(allRate + "%");
                trainPersonrate.setText(personRate + "%");
                trainExplain.setText(explain);
                testKeys = bean.getData();

                sequenceWrongPeople.setText(allRate + "%");
                sequenceWrongMyRight.setText(myRight);
                sequenceWrongMyWrong.setText(myWrong);

                sequenceExplainLayout.setVisibility(View.GONE);
                sequenceWrongLayout.setVisibility(View.VISIBLE);
                saveInfo();
            }
//            isVisible = !isVisible;
        }
    }

    private void initClickBtn() {
        trainSelectA.setOnClickListener(this);
        trainSelectB.setOnClickListener(this);
        trainSelectC.setOnClickListener(this);
        trainSelectD.setOnClickListener(this);
        trainSelectF.setOnClickListener(this);
        trainSelectE.setOnClickListener(this);
        trainJudgeA.setOnClickListener(this);
        trainJudgeB.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public String getTtype() {
        return ttype;
    }

    public void setTtype(String ttype) {
        this.ttype = ttype;
//        if (type == 3) {
        initType();
//        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
//        if (type == 3) {
        trainTitle.setText(title);
//        }
    }

    public List<String> getRight() {
        return right;
    }

    public void setRight(List<String> right) {

        this.right = right;
    }

    public void setExplain(String explain) {
        trainExplain.setText(explain);
    }

    @Override
    public void onClick(View v) {

        if (Integer.parseInt(ttype) == 3) {
            switch (v.getId()) {
                case R.id.train_select_a: {

                    addOrDeleSelectResult(1);

                    break;
                }
                case R.id.train_select_b: {
                    addOrDeleSelectResult(2);
                    break;
                }
                case R.id.train_select_c: {
                    addOrDeleSelectResult(3);
                    break;
                }
                case R.id.train_select_d: {
                    addOrDeleSelectResult(4);

                    break;
                }
                case R.id.train_select_e: {
                    addOrDeleSelectResult(5);
                    break;
                }
                case R.id.train_select_f: {
                    addOrDeleSelectResult(6);
                    break;
                }
            }
        } else {
            switch (v.getId()) {
                case R.id.train_select_a: {
                    initSelectResult(1);
                    exerciseActivity.addQuizChoose(position, 1);
                    checkIsAllAnswer();
                    break;
                }
                case R.id.train_select_b: {
                    initSelectResult(2);
                    exerciseActivity.addQuizChoose(position, 2);
                    checkIsAllAnswer();
                    break;
                }
                case R.id.train_select_c: {
                    initSelectResult(3);
                    exerciseActivity.addQuizChoose(position, 3);
                    checkIsAllAnswer();

                    break;
                }
                case R.id.train_select_d: {
                    initSelectResult(4);
                    exerciseActivity.addQuizChoose(position, 4);
                    checkIsAllAnswer();

                    break;
                }
                case R.id.train_select_e: {
                    initSelectResult(5);
                    exerciseActivity.addQuizChoose(position, 5);
                    checkIsAllAnswer();

                    break;
                }
                case R.id.train_select_f: {
                    initSelectResult(6);
                    exerciseActivity.addQuizChoose(position, 6);
                    checkIsAllAnswer();

                    break;
                }
                case R.id.train_judge_a: {
                    initJudgeResult(1);
                    exerciseActivity.addQuizChoose(position, 1);
                    checkIsAllAnswer();

                    break;
                }
                case R.id.train_judge_b: {
                    initJudgeResult(2);
                    exerciseActivity.addQuizChoose(position, 2);
                    checkIsAllAnswer();
                    break;
                }
            }
        }
    }

    private void addOrDeleSelectResult(int select) {

        //判断数组中是否有
        boolean flag = false;
        for (int i1 = 0; i1 < choose.size(); i1++) {
            if (Integer.valueOf(choose.get(i1)) == select) {
                choose.remove(i1);
                flag = true;
            }
        }
        if (flag = false) {
            choose.add(String.valueOf(select));
        }
//        if (results.toString().contains(String.valueOf(i))) {
//            result = results.toString().replace(String.valueOf(i), "");
//        } else {
//            results.append(String.valueOf(String.valueOf(i)));
//        }

    }


    public void setPosition(int position) {
        this.position = position;
    }

    private void initQuizNum() {
        trainSelectA.setVisibility(View.GONE);
        trainSelectB.setVisibility(View.GONE);
        trainSelectC.setVisibility(View.GONE);
        trainSelectD.setVisibility(View.GONE);
        trainSelectE.setVisibility(View.GONE);
        trainSelectF.setVisibility(View.GONE);
        switch (quizNum) {
            case 1:
                trainSelectA.setVisibility(View.VISIBLE);
                break;
            case 2:
                trainSelectA.setVisibility(View.VISIBLE);
                trainSelectB.setVisibility(View.VISIBLE);
                break;
            case 3:
                trainSelectA.setVisibility(View.VISIBLE);
                trainSelectB.setVisibility(View.VISIBLE);
                trainSelectC.setVisibility(View.VISIBLE);
                break;
            case 4:
                trainSelectA.setVisibility(View.VISIBLE);
                trainSelectB.setVisibility(View.VISIBLE);
                trainSelectC.setVisibility(View.VISIBLE);
                trainSelectD.setVisibility(View.VISIBLE);
                break;
            case 5:
                trainSelectA.setVisibility(View.VISIBLE);
                trainSelectB.setVisibility(View.VISIBLE);
                trainSelectC.setVisibility(View.VISIBLE);
                trainSelectD.setVisibility(View.VISIBLE);
                trainSelectE.setVisibility(View.VISIBLE);
                break;
            case 6:
                trainSelectA.setVisibility(View.VISIBLE);
                trainSelectB.setVisibility(View.VISIBLE);
                trainSelectC.setVisibility(View.VISIBLE);
                trainSelectD.setVisibility(View.VISIBLE);
                trainSelectE.setVisibility(View.VISIBLE);
                trainSelectF.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void checkIsAllAnswer() {
        if (exerciseActivity.isAllAnswer()) {
            AlertDialog.Builder build = new AlertDialog.Builder(getActivity());
            build.setTitle("提示");
            build.setMessage("您已做完所有题目,是否退出");
            build.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    exerciseActivity.finish();
                }
            });
            build.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            build.show();
        }
    }

    private void initSelectResult(int result) {
        choose.add(String.valueOf(result));
        switch (type) {
            case Constant.SEQUENCE: {
                int rightAnswer;
                if (type == 3) {
                    StringBuffer results = new StringBuffer();
                    for (int i = 0; i < right.size(); i++) {
                        results.append(right.get(i));
                    }
                    rightAnswer = Integer.parseInt(results.toString());
                } else {
                    rightAnswer = Integer.parseInt(right.get(0));
                }
                if (result == rightAnswer) {
                    isRightInt = 1;
                    if (exerciseActivity == null) {
                        exerciseActivity = (ExerciseActivity) getActivity();
                        exerciseActivity.addQuizCorrect(ttype, id, String.valueOf(rightAnswer), true, position);
                    } else {
                        exerciseActivity.addQuizCorrect(ttype, id, String.valueOf(result), true, position);
                    }
                    isRight = true;

                    userPresenter.sequenceKeys(position, id, ttype, libtype, "1", String.valueOf(result), userid);
                    DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
                    trainExplainRate.setVisibility(View.VISIBLE);
                    trainExplainDetail.setVisibility(View.VISIBLE);
//                    TimerTask timerTask = new TimerTask() {
//                        @Override
//                        public void run() {
//                            exerciseActivity.selectRightAnswer(position + 1);
//                        }
//                    };
//
//                    timer.schedule(timerTask, 1000);

                    for (int i = 0; i < String.valueOf(result).length(); i++) {
                        switch (String.valueOf(result).charAt(i)) {
                            case '1': {
                                trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case '2': {
                                trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case '3': {
                                trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case '4': {
                                trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case '5': {
                                trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case '6': {
                                trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            default:
                                trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                break;
                        }

                    }
                } else {
                    isRightInt = 0;
                    isRight = false;

                    userPresenter.sequenceKeys(position, id, ttype, libtype, "0", String.valueOf(result), userid);
                    trainExplainRate.setVisibility(View.VISIBLE);
                    trainExplainDetail.setVisibility(View.VISIBLE);

                    if (exerciseActivity == null) {
                        exerciseActivity = (ExerciseActivity) getActivity();
                    }

                    exerciseActivity.addQuizCorrect(ttype, id, String.valueOf(rightAnswer), false, position);
                    switch (rightAnswer) {
                        case 1: {
                            drawSelectResult(trainSelectIconA);
                            break;
                        }
                        case 2: {
                            drawSelectResult(trainSelectIconB);
                            break;
                        }
                        case 3: {
                            drawSelectResult(trainSelectIconC);
                            break;
                        }
                        case 4: {
                            drawSelectResult(trainSelectIconD);
                            break;
                        }
                        case 5: {
                            drawSelectResult(trainSelectIconE);
                            break;
                        }
                        case 6: {
                            drawSelectResult(trainSelectIconF);
                            break;
                        }
                        default:
                            for (int i = 0; i < String.valueOf(rightAnswer).length(); i++) {
                                switch (String.valueOf(rightAnswer).charAt(i)) {
                                    case '1': {
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '2': {
                                        trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '3': {
                                        trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '4': {
                                        trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '5': {
                                        trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '6': {
                                        trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    default:
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                }
                                break;
                            }
                    }
                }
                break;
            }

            case Constant.COLLECT: {
                isSelected = true;
                StringBuffer selectRight = new StringBuffer();
                trainSelectA.setClickable(false);
                trainSelectB.setClickable(false);
                trainSelectC.setClickable(false);
                trainSelectD.setClickable(false);
                trainSelectE.setClickable(false);
                trainSelectF.setClickable(false);
                trainJudgeA.setClickable(false);
                trainJudgeB.setClickable(false);

                this.result = String.valueOf(result);
                for (int i = 0; i < right.size(); i++) {
                    selectRight.append(right.get(i));
                }
                int selectAnswer = Integer.parseInt(selectRight.toString());
                if (result == selectAnswer) {
                    if (!isSelected) {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(result), "1");
                        if (!exerciseActivity.isAllAnswer()) {
                            if (position < total - 1) {
                                exerciseActivity.addQuizCorrect(position, true);
//                                TimerTask timerTask = new TimerTask() {
//                                    @Override
//                                    public void run() {
//                                        exerciseActivity.selectRightAnswer(position + 1);
//                                    }
//                                };
//                                timer.schedule(timerTask, 1000);
                                DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
                            }
                        }
                    }
                    switch (result) {
                        case 1: {
                            trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 3: {
                            trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 4: {
                            trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 5: {
                            trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 6: {
                            trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    if (!isSelected) {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(result), "0");
                    }
                    exerciseActivity.addQuizCorrect(position, false);
                    switch (selectAnswer) {
                        case 1: {
                            drawSelectResult(trainSelectIconA);
                            break;
                        }
                        case 2: {
                            drawSelectResult(trainSelectIconB);
                            break;
                        }
                        case 3: {
                            drawSelectResult(trainSelectIconC);
                            break;
                        }
                        case 4: {
                            drawSelectResult(trainSelectIconD);
                            break;
                        }
                        case 5: {
                            drawSelectResult(trainSelectIconE);
                            break;
                        }
                        case 6: {
                            drawSelectResult(trainSelectIconF);
                            break;
                        }
                    }
                }
                isSelected = true;

                break;
            }

            case Constant.TRAIN_ERROR: {
                isSelected = true;
                StringBuffer selectRight = new StringBuffer();
                trainSelectA.setClickable(false);
                trainSelectB.setClickable(false);
                trainSelectC.setClickable(false);
                trainSelectD.setClickable(false);
                trainSelectE.setClickable(false);
                trainSelectF.setClickable(false);
                trainJudgeA.setClickable(false);
                trainJudgeB.setClickable(false);

                this.result = String.valueOf(result);
                for (int i = 0; i < right.size(); i++) {
                    selectRight.append(right.get(i));
                }
                int selectAnswer = Integer.parseInt(selectRight.toString());
                if (result == selectAnswer) {

                    if (!isSelected) {
                        switch (type) {
                            case 1: {
                                userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "1", String.valueOf(result), userid);
                                break;
                            }
                            case 2: {
                                userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "1", String.valueOf(result), userid);
                                break;
                            }
                            case 3: {
                                userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(result), "1");
                                break;
                            }
                        }

                        if (!exerciseActivity.isAllAnswer()) {
                            if (position < total - 1) {
                                exerciseActivity.addQuizCorrect(position, true);
//                                TimerTask timerTask = new TimerTask() {
//                                    @Override
//                                    public void run() {
//                                        exerciseActivity.selectRightAnswer(position + 1);
//                                    }
//                                };
//                                timer.schedule(timerTask, 1000);
                                DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
                            }
                        }
                    }
                    switch (result) {
                        case 1: {
                            trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 2: {
                            trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 3: {
                            trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 4: {
                            trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 5: {
                            trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                        case 6: {
                            trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                            break;
                        }
                    }
                } else {
                    if (!isSelected) {
                        switch (type) {
                            case 1: {
                                userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "0", String.valueOf(result), userid);
                                break;
                            }
                            case 2: {
                                userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "0", String.valueOf(result), userid);
                                break;
                            }
                            case 3: {
                                userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(choose), "0");
                                break;
                            }
                        }
                    }
                    exerciseActivity.addQuizCorrect(position, false);
                    switch (selectAnswer) {
                        case 1: {
                            drawSelectResult(trainSelectIconA);
                            break;
                        }
                        case 2: {
                            drawSelectResult(trainSelectIconB);
                            break;
                        }
                        case 3: {
                            drawSelectResult(trainSelectIconC);
                            break;
                        }
                        case 4: {
                            drawSelectResult(trainSelectIconD);
                            break;
                        }
                        case 5: {
                            drawSelectResult(trainSelectIconE);
                            break;
                        }
                        case 6: {
                            drawSelectResult(trainSelectIconF);
                            break;
                        }
                    }
                }
                isSelected = true;
            }
            break;
        }
    }


    private void initSelect() {
        trainSelectIconA.setImageResource(R.mipmap.exam_a);
        trainSelectIconB.setImageResource(R.mipmap.exam_b);
        trainSelectIconC.setImageResource(R.mipmap.exam_c);
        trainSelectIconD.setImageResource(R.mipmap.exam_d);
        trainSelectIconE.setImageResource(R.mipmap.exam_e);
        trainSelectIconF.setImageResource(R.mipmap.exam_f);
        trainJudgeIconA.setImageResource(R.mipmap.exam_a);
        trainJudgeIconB.setImageResource(R.mipmap.exam_b);
    }

    private void drawSelectResult(ImageView select) {
        trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
        trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
        drawSelectsResult(select);
    }

    private void drawSelectsResult(ImageView select) {

        select.setImageResource(R.mipmap.exam_answer_right);
    }

    private void initJudgeResult(int result) {
        isSelectAnswer = true;
        StringBuffer selectRight = new StringBuffer();
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        trainSelectE.setClickable(false);
        trainSelectF.setClickable(false);

        this.result = String.valueOf(result);
        for (int i = 0; i < right.size(); i++) {
            selectRight.append(right.get(i));
        }
        int judgeAnswer = Integer.parseInt(selectRight.toString());
        if (result == judgeAnswer) {
            if (!isSelected) {
                switch (type) {
                    case 1: {
                        userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "1", String.valueOf(result), userid);
                        break;
                    }
                    case 2: {
                        userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "1", String.valueOf(result), userid);
                        break;
                    }
                    case 3: {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(result), "1");
                        break;
                    }
                }

                if (!exerciseActivity.isAllAnswer()) {
                    if (position < total - 1) {
//                        TimerTask timerTask = new TimerTask() {
//                            @Override
//                            public void run() {
//                                exerciseActivity.selectRightAnswer(position + 1);
//                            }
//                        };
//                        timer.schedule(timerTask, 1000);
                        DeviceUtil.MyToast(getActivity(), Constant.ANSWER_RIGHT);
                    }
                }
            }
            exerciseActivity.addQuizCorrect(position, true);

            switch (result) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            if (!isSelected) {
                switch (type) {
                    case 1: {
                        userPresenter.sequenceKeys(position, dataEntity.getId(), dataEntity.getTtype(), libtype, "0", String.valueOf(result), userid);
                        break;
                    }
                    case 2: {
                        userPresenter.errorKeys(String.valueOf(position + 1), id, ttype, libtype, "0", String.valueOf(result), userid);
                        break;
                    }
                    case 3: {
                        userPresenter.CollectKey(String.valueOf(position + 1), userid, libtype, String.valueOf(choose), "0");
                        break;
                    }
                }
            }
            exerciseActivity.addQuizCorrect(position, false);
            switch (judgeAnswer) {
                case 1: {
                    drawJudgeRuselt(trainJudgeIconA);
                    break;
                }
                case 2: {
                    drawJudgeRuselt(trainJudgeIconB);
                    break;
                }
            }
        }
        isSelected = true;
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    private void saveInfo() {
        if (collectAnswerBean != null) {
            collectAnswerBean.setTtype(ttype);
            collectAnswerBean.setTitle(title);
            collectAnswerBean.setOption(option);
            collectAnswerBean.setRight(right);
            collectAnswerBean.setIsRight(isRight);
            collectAnswerBean.setExplain(explain);
            collectAnswerBean.setAllrate(allRate);
            collectAnswerBean.setPersonrate(personRate);
            collectAnswerBean.setChoose(choose);
            collectAnswerBean.setOk_times(myRight);
            collectAnswerBean.setWrong_times(myWrong);
        }
    }

    private void initInfo(SequenceAnswerBean bean) {
        trainJudgeA.setClickable(false);
        trainJudgeB.setClickable(false);
        trainSelectA.setClickable(false);
        trainSelectB.setClickable(false);
        trainSelectC.setClickable(false);
        trainSelectD.setClickable(false);
        trainSelectE.setClickable(false);
        trainSelectF.setClickable(false);
        String ttype = dataEntity.getTtype();
        if (!(ttype == null || ttype.isEmpty())) {
            switch (Integer.parseInt(ttype)) {
                case 1: {
                    switch (bean.getIsRight()) {
                        case "0": {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate()));
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate()));

                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());

                            switch (dataEntity.getRight().get(0)) {

                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                        case "1": {
                            switch (dataEntity.getRight().get(0)) {
                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
                case 2: {
                    switch (bean.getIsRight()) {
                        case "0": {
                            switch (bean.getChoose().get(0)) {
                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                        case "1": {
                            switch (bean.getChoose().get(0)) {
                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                        }
                    }
                }
                case 3: {
                    StringBuffer rightChoose = new StringBuffer();
                    switch (bean.getIsRight()) {
                        case "0": {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate()));
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate()));

                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());

                            trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                            for (int i = 0; i < dataEntity.getRight().size(); i++) {
                                switch (dataEntity.getRight().size()) {
                                    case '1': {
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '2': {
                                        trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '3': {
                                        trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '4': {
                                        trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '5': {
                                        trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case '6': {
                                        trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    default:
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                }
                                break;
                            }
                        }
                        case "1": {
                            switch (dataEntity.getRight().get(0)) {
                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }

    private void initInfo(CollectAnswerBean bean) {
        if (isSelected) {
            trainJudgeA.setClickable(false);
            trainJudgeB.setClickable(false);
            trainSelectA.setClickable(false);
            trainSelectB.setClickable(false);
            trainSelectC.setClickable(false);
            trainSelectD.setClickable(false);
            trainSelectE.setClickable(false);
            trainSelectF.setClickable(false);

            ttype = bean.getTtype();
            if (!(ttype == null || ttype.isEmpty())) {
                switch (Integer.parseInt(ttype)) {
                    case 1: {
                        if (!bean.getIsRight()) {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate())+"%");
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate())+"%");
                            sequenceWrongMyRight.setText(bean.getOk_times());
                            sequenceWrongMyWrong.setText(bean.getWrong_times());
                            sequenceWrongPeople.setText(String.valueOf(bean.getAllrate()) + "%");
                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());

                            switch (bean.getRight().get(0)) {

                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                        } else {
                            switch (bean.getRight().get(0)) {
                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    case 2: {
                        if (bean.getIsRight()) {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate())+"%");
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate())+"%");
                            sequenceWrongMyRight.setText(bean.getOk_times());
                            sequenceWrongMyWrong.setText(bean.getWrong_times());
                            sequenceWrongPeople.setText(String.valueOf(bean.getAllrate()) + "%");
                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());
                            switch (bean.getRight().get(0)) {

                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                        } else {
                            switch (bean.getRight().get(0)) {
                                case "1": {
                                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                        }
                    }

                    case 3: {
                        StringBuffer rightChoose = new StringBuffer();
                        if (!bean.getIsRight()) {
                            trainExplainRate.setVisibility(View.VISIBLE);
                            trainAllrate.setText(String.valueOf(bean.getAllrate())+"%");
                            trainPersonrate.setText(String.valueOf(bean.getPersonrate())+"%");
                            sequenceWrongMyRight.setText(bean.getOk_times());
                            sequenceWrongMyWrong.setText(bean.getWrong_times());
                            sequenceWrongPeople.setText(String.valueOf(bean.getAllrate()) + "%");
                            trainExplainDetail.setVisibility(View.VISIBLE);
                            trainExplain.setText(bean.getExplain());

                            trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                            trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                            for (int i = 0; i < bean.getChoose().size(); i++) {
                                switch (bean.getChoose().get(i)) {
                                    case "1": {
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case "2": {
                                        trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case "3": {
                                        trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case "4": {
                                        trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case "5": {
                                        trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    case "6": {
                                        trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                    }
                                    default:
                                        trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                        break;
                                }

                            }
                        } else {
                            switch (dataEntity.getRight().get(0)) {
                                case "1": {
                                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "2": {
                                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "3": {
                                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "4": {
                                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "5": {
                                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                                case "6": {
                                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                                    break;
                                }
                            }
                            break;
                        }

                    }
                }
            }
        } else {
            explain = bean.getExplain();
            ttype = bean.getTtype();
            right = bean.getRight();
            title = bean.getTitle();
            option = bean.getOption();
            initType();
            initTitle();
            initOptions();
            initQuizNum();
            initClickBtn();
        }
    }

    private void initJudgeAnswer() {
        if (isRight) {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                }
            }
        } else {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 2: {
                    trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        }
    }

    private void initSelectAnswer() {
        if (isRight) {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }

                case 4: {
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 5: {
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 6: {
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 2: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 3: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 4: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;

                }
                case 5: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 6: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                default:
                    break;
            }
        }
    }

    private void initSelectsAnswer() {
        if (isRight) {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 4: {
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 5: {
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 6: {
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
            switch (Integer.parseInt(right.get(0))) {
                case 1: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 2: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 3: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 4: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;

                }
                case 5: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
                    break;
                }
                case 6: {
                    trainSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
                    trainSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                default:
                    break;
            }
        }
    }

    public void showDetail() {
        sequenceExplainAllrate.setText(allRate + "%");
        sequenceExplainExplain.setText(explain);
        sequenceExplainMyRight.setText(myRight);
        sequenceExplainMyWrong.setText(myWrong);
        sequenceExplainPeopleWrong.setText(allRate + "%");
        sequenceExplainPersonrate.setText(personRate + "%");

        sequenceWrongLayout.setVisibility(View.GONE);
        sequenceExplainLayout.setVisibility(View.VISIBLE);

        isCheckExplain = !isCheckExplain;
    }

    private void drawJudgeRuselt(ImageView judge) {
        trainJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
        trainJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);

        judge.setImageResource(R.mipmap.exam_answer_right);
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
        if (isVisible) {
            timer = new Timer();
            userPresenter = new UserPresenterImp();
            if (!notLoginGetAnswer) {
                userPresenter.sequenceTest(position, libtype, userid);
            } else {
                SequenceAnswerBean bean1 = new SequenceAnswerBean();
                bean1.setChoose(choose);
                bean1.setIsRight(String.valueOf(isRightInt));
                initType();
                initTitle();
                initOptions();
                initQuizNum();
                initInfo(bean1);
                if (!isCheckExplain) {
                    trainExplain.setText(explain);
                    trainAllrate.setText(allRate + "%");
                    sequenceWrongPeople.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceWrongMyWrong.setText("0");
                        sequenceWrongMyRight.setText("0");
                        trainPersonrate.setText("0%");
                    } else {
                        trainPersonrate.setText(personRate + "%");
                        sequenceWrongMyWrong.setText(myWrong);
                        sequenceWrongMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.GONE);
                    sequenceWrongLayout.setVisibility(View.VISIBLE);
                } else {
                    sequenceExplainExplain.setText(explain);
                    sequenceExplainAllrate.setText(allRate + "%");
                    sequenceExplainPeopleWrong.setText(allRate + "%");
                    if (notLoginGetAnswer) {
                        sequenceExplainMyRight.setText("0");
                        sequenceExplainMyWrong.setText("0");
                        sequenceExplainPersonrate.setText("0%");
                    } else {
                        sequenceExplainPersonrate.setText(personRate + "%");
                        sequenceExplainMyWrong.setText(myWrong);
                        sequenceExplainMyRight.setText(myRight);
                    }
                    sequenceExplainLayout.setVisibility(View.VISIBLE);
                    sequenceWrongLayout.setVisibility(View.GONE);
                }
            }
            if (sequenceAnswerBean != null) {
                initInfo(sequenceAnswerBean);
            }
        } else {
            if (timer != null) {
                timer.cancel();
                timer.purge();
            }
        }
    }


    private void initRate(SequenceKeysBean.DataEntity dataEntity) {
        trainAllrate.setText(String.valueOf(allRate) + "%");
        trainPersonrate.setText(String.valueOf(personRate) + "%");
        sequenceWrongMyRight.setText(String.valueOf(myRight));
        sequenceWrongMyWrong.setText(String.valueOf(myWrong));
        sequenceWrongPeople.setText(String.valueOf(allRate) + "%");
    }

    private void initExplain(SequenceKeysBean.DataEntity dataEntity) {
        trainExplain.setText(dataEntity.getExplain());
    }

    public String getTid() {
        return tid;
    }

    /**
     * 获取答案
     */
    public void onEventMainThread(SequenceKeysBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                sequenceSyncForLoginBean = new SequenceSyncForLoginBean();
                sequenceSyncForLoginBean.setChoose(String.valueOf(choose));
                sequenceSyncForLoginBean.setCount(String.valueOf(position + 1));
                String isright = isRight ? "1" : "0";
                sequenceSyncForLoginBean.setIsright(isright);
                sequenceSyncForLoginBean.setTid(dataEntity.getId());
                sequenceSyncForLoginBean.setTtype(dataEntity.getTtype());

                allRate = bean.getData().getAllrate();
                if (bean.getData().getPersonrate() != null) {
                    personRate = bean.getData().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getData().getExplain();

                if (myRight != null) {
                    myRight = bean.getData().getOk_times();
                } else {
                    myRight = "0";
                }

                if (myWrong != null) {
                    myWrong = bean.getData().getWrong_times();
                } else {
                    myWrong = "0";
                }

                if (isVisible) {
                    if (userid == null || userid.isEmpty()) {
                        notLoginGetAnswer = true;
                        personRate = "0";
                        myRight = "0";
                        myWrong = "0";
                    }
                }

                initExplain(bean.getData());
                initRate(bean.getData());

                sequenceExplainLayout.setVisibility(View.GONE);
                sequenceWrongLayout.setVisibility(View.VISIBLE);

                if (!isRight) {
                    SequenceAnswerBean sequenceAnswerBean = new SequenceAnswerBean();
                    sequenceAnswerBean.setPersonrate(bean.getData().getPersonrate());
                    sequenceAnswerBean.setAllrate(bean.getData().getAllrate());
                    sequenceAnswerBean.setExplain(bean.getData().getExplain());
                    sequenceAnswerBean.setChoose(dataEntity.getRight());
                    sequenceAnswerBean.setIsRight(String.valueOf(isRightInt));
                    if (exerciseActivity == null) {
                        exerciseActivity = (ExerciseActivity) getActivity();
                    }
                    exerciseActivity.addQuizDetail(sequenceAnswerBean, position);
                } else {
                    SequenceAnswerBean sequenceAnswerBean = new SequenceAnswerBean();
                    sequenceAnswerBean.setChoose(choose);
                    sequenceAnswerBean.setIsRight(String.valueOf(isRightInt));

                    if (exerciseActivity == null) {
                        exerciseActivity = (ExerciseActivity) getActivity();
                    }
                    exerciseActivity.addQuizDetail(sequenceAnswerBean, position);
                }

                if (ischecked) {
                    initExplain(bean.getData());
                    sequenceExplainLayout.setVisibility(View.VISIBLE);
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }
        }
    }

    public void onEventMainThread(MyTestCollectBean bean) {
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                collectDataEntity = bean.getData();
                myRight = bean.getData().getOk_times();
                myWrong = bean.getData().getWrong_times();
                ttype = bean.getData().getTtype();
                tid = bean.getData().getId();
                right = bean.getData().getRight();
                title = bean.getData().getTitle();
                allRate = bean.getData().getAllrate();
                option = bean.getData().getOption();
                id = bean.getData().getId();
                if (bean.getData().getPersonrate() != null) {
                    personRate = bean.getData().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getData().getExplain();
                if (bean.getData().getWrong_times() != null) {
                    myWrong = bean.getData().getWrong_times();
                } else {
                    myWrong = "0";
                }
                initType();
                initTitle();
                initOptions();
                initQuizNum();

                if (isSelected) {
                    if (bean.getData().getTtype().equals("1")) {
                        initSelectAnswer();
                    } else if (bean.getData().getTtype().equals("2")) {
                        initJudgeAnswer();
                    } else {
                        StringBuffer answer = new StringBuffer();
                        for (int i = 0; i < choose.size(); i++) {
                            answer.append(choose.get(i));
                        }
                        initSelectsResult(answer.toString());
                    }
                    checkExplain();
                } else {
                    initClickBtn();
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }
            saveInfo();
        }
    }

    /**
     * 获取题目
     */
    public void onEventMainThread(SequenceTestBean bean) {
        //判断 isVisible 是因为 viewpager 会一次性加载三个 Fragment, 所以要判断 Fragmnet 是不是 已显示
        if (isVisible) {
            if (bean.getReturn_code() == 0) {
                tid = bean.getData().getId();
                ttype = bean.getData().getTtype();
                if (exerciseActivity == null) {
                    exerciseActivity = (ExerciseActivity) getActivity();
                }
                exerciseActivity.isCollect(bean.getRecord().getIsCollect().equals("1"));
                isCollect = bean.getRecord().getIsCollect().equals("1");

                dataEntity = bean.getData();
                option = bean.getData().getOption();
                right = bean.getData().getRight();
                title = bean.getData().getTitle();
                id = bean.getData().getId();
                initType();//判断是选择题还是判断题
                initTitle();//显示题目
                initOptions();//显示选项
                initQuizNum();//设置选项个数

                allRate = bean.getRecord().getAllrate();

                if (bean.getRecord().getPersonrate() != null) {
                    personRate = bean.getRecord().getPersonrate();
                } else {
                    personRate = "0";
                }
                explain = bean.getRecord().getExplain();
                if (bean.getRecord().getWrong_times() != null) {
                    myWrong = bean.getRecord().getWrong_times();
                } else {
                    myWrong = "0";
                }
                if (bean.getRecord().getOk_times() != null) {
                    myRight = bean.getRecord().getOk_times();
                } else {
                    myRight = "0";
                }

                if (userid != null && !userid.isEmpty()) {
                    if (!bean.getRecord().getChoose().equals("0")) {
                        isSelectAnswer = true;
                    } else {
                        isSelectAnswer = false;
                    }
                }
                if (!isSelectAnswer) {
                    initClickBtn();
                    initSelect();
                    sequenceExplainLayout.setVisibility(View.GONE);
                    sequenceWrongLayout.setVisibility(View.GONE);
                } else {
                    if (isVisible) {
                        SequenceAnswerBean bean1 = new SequenceAnswerBean();
                        List<String> list = new ArrayList<>();
                        for (int i = 0; i < bean.getRecord().getChoose().length(); i++) {
                            list.add(String.valueOf(bean.getRecord().getChoose().charAt(i)));
                        }
                        bean1.setChoose(list);
                        bean1.setIsRight(bean.getRecord().getIsright());
                        initInfo(bean1);

                        if (!isCheckExplain) {
                            trainExplain.setText(explain);
                            trainAllrate.setText(allRate + "%");
                            sequenceWrongPeople.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceWrongMyWrong.setText("0");
                                sequenceWrongMyRight.setText("0");
                                trainPersonrate.setText("0%");
                            } else {
                                trainPersonrate.setText(personRate + "%");
                                sequenceWrongMyWrong.setText(myWrong);
                                sequenceWrongMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.GONE);
                            sequenceWrongLayout.setVisibility(View.VISIBLE);
                        } else {
                            sequenceExplainExplain.setText(explain);
                            sequenceExplainAllrate.setText(allRate + "%");
                            sequenceExplainPeopleWrong.setText(allRate + "%");
                            if (notLoginGetAnswer) {
                                sequenceExplainMyRight.setText("0");
                                sequenceExplainMyWrong.setText("0");
                                sequenceExplainPersonrate.setText("0%");
                            } else {
                                sequenceExplainPersonrate.setText(personRate + "%");
                                sequenceExplainMyWrong.setText(myWrong);
                                sequenceExplainMyRight.setText(myRight);
                            }
                            sequenceExplainLayout.setVisibility(View.VISIBLE);
                            sequenceWrongLayout.setVisibility(View.GONE);
                        }
                    }
                }
            } else {
                DeviceUtil.MyToast(getActivity(), bean.getMsg());
            }
        }
    }

    public void setCollect(int i) {
        if (i == position) {
            collectPosition = i;
            if (dataEntity != null) {
                userPresenter.testCollect(dataEntity.getId(), dataEntity.getTtype(), libtype, userid, String.valueOf("0"), String.valueOf(choose));
            } else {
                DeviceUtil.MyToast(getActivity(), "还未获取题目");
            }
        }
    }
}
